package com.backwaveweibo.bww.service.impl;

import com.backwaveweibo.bww.cache.ApplicationContextHolder;
import com.backwaveweibo.bww.entity.User;
import com.backwaveweibo.bww.mapper.UserMapper;
import com.backwaveweibo.bww.service.FollowService;
import com.backwaveweibo.bww.utils.RedisUtil;


import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 靓仔王德发
 * @date 六月八号
 * Redis关注操作
 */
//@Service
public class FollowServiceImpl implements FollowService {
    @Resource
    RedisUtil redisUtil;

    @Resource
    UserMapper userMapper;
    //爷的粉丝
    private static final String MY_FANS = "USER_%s:MY_FANS";
    //爷的关注
    private static final String MY_SUBSCRIBE = "USER_%s:MY_SUBSCRIBE";

    private RedisUtil getRedisUtil(){
        if (redisUtil == null){
            redisUtil = ApplicationContextHolder.getBean("redisUtil");
        }
        return redisUtil;
    }

    /**
     *
     * @param fansId 传入id
     * @param user 传入用户
     * @解释 判断这个用户是否关注了这个id所代表的用户
     * @return
     */
    @Override
    public Boolean isFans(Long fansId, User user) {
        //格式化key
        String fanUsersKey = String.format(MY_SUBSCRIBE, user.getUserId());
        Object value = redisUtil.hget(fanUsersKey, String.valueOf(fansId));
        return value != null ;
    }

    @Override
    public Boolean isSubscribe(Long subscribeId, User user) {
        return false;
    }

    /**
     * 关注功能
     * @param followId 传入要关注的人的id
     * @param user  操作主体
     */
    @Override
    public Boolean setFollow(Long followId, User user){
        redisUtil = getRedisUtil();
        User followUser = userMapper.selectUser(new User().setUserId(followId));
        //User followUser = new User().setUserId((long) 1).setUsername("我是被关注的人");
        //给自己的关注增加一个人
        String usersKey = String.format(MY_SUBSCRIBE, user.getUserId());
        redisUtil.hset(usersKey, String.valueOf(followId), followUser.getUsername());

        //给我关注的人增加一个粉丝
        String fanUsersKey = String.format(MY_FANS, followId);
        redisUtil.hset(fanUsersKey, String.valueOf(user.getUserId()), user.getUsername());
        return true;
    }

    /**
     * 取消关注功能
     * @param unFollowId 这是我想要取消关注人的id
     * @param user  这是爷
     */
    @Override
    public Boolean setUnFollow(Long unFollowId, User user){
        User unFollowUser = userMapper.selectUser(new User().setUserId(unFollowId));


        //给自己的关注减少一个人
        String followUsersKey = String.format(MY_SUBSCRIBE, user.getUserId());
        redisUtil.hdel(followUsersKey, String.valueOf(unFollowId), unFollowUser.getUsername());

        //给我关注的人减少一个粉丝
        String fanUsersKey = String.format(MY_FANS, unFollowId);
        redisUtil.hdel(fanUsersKey, String.valueOf(user.getUserId()), user.getUsername());
        return true;
    }

    @Override
    public Boolean delUserKey(Long id) {
        return null;
    }

    /**
     * 获取爷的关注
     * @param id
     * @return
     */
    
    @Override
    public List<Long> getMySubscribe(Long id){
        String fanUsersKey = String.format(MY_SUBSCRIBE,id);
        Map<Object, Object> myFollowers = redisUtil.hmget(fanUsersKey);
        return null;
    }

    /**
     * 获取爷的粉丝
     * @param user
     * @return
     */
    @Override
    public List<Long> getMyFans(User user){
        String fanUsersKey = String.format(MY_FANS,user.getUserId());
        Map<Object, Object> hmget = redisUtil.hmget(fanUsersKey);
        return null;
    }

    /**
     * 获取爷关注人的数量
     * @param user
     * @return
     */
    @Override
    public Long myFollowCount(User user){
       String fanUsersKey = String.format(MY_SUBSCRIBE,user.getUserId());
        return redisUtil.hLen(fanUsersKey);

    }

    /**
     * 获取关注爷的人的数量
     * @param user
     * @return
     */

    @Override
    public Long followMeCount(User user){
        redisUtil = getRedisUtil();
        String fanUsersKey = String.format(MY_FANS,user.getUserId());
        return redisUtil.hLen(fanUsersKey);
    }



}
